layui.use(['layer','form','table','element'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element;

    var projectId = $("#pid").val();

    //初始化数据表格
     table.render({
         elem:"#data"
         ,height: 340
         ,url:"/admin/guaranteeProject/getGuranteeUserList?projectId="+projectId
         ,cellMinWidth: 80
         ,cols: [[
             {field:'id',width:60,title: 'ID', sort: true}
             ,{field:'qq', title: '用户QQ'}
             ,{field:'phone'   , title: '用户手机'}
             ,{field:'showCreateTime',title: '创建时间',sort: true}
             ,{field:'',title: '操作',toolbar: '#barDemo'}
         ]]
         ,page:true
     });

     //监听工具条
     table.on('tool(dataTable)',function (obj) {
         var data = obj.data;
        if(obj.event == 'del'){
            //删除项目

             layer.confirm('确定删除？', function(index){
                 layer.msg('删除中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

                 $.ajax({
                    type:"POST"
                     ,url:"/admin/guaranteeProject/deleteUser"
                     ,data:{
                        id:data.id,
                         projectId:data.projectId
                     }
                     ,success:function (result) {
                         layer.msg(result.message);
                         table.reload("data");
                     }
                     ,error:function () {
                         layer.msg("系统错误");
                     }

                 });

             });
         } else {
             //编辑项目
            layer.msg("操作错误！");
         }
     });

    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }
     
     //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                qq:$("#qq").val(),
                phone:$("#phone").val(),

            }
        })
    });;

    $("#add").on("click",function () {

        parent.layui.element.tabAdd('tabs',{
            title:"添加包赔用户信息",
            content:"<iframe  src='/admin/guaranteeProject/toAddUser/"+projectId+"'  frameborder='0'  style='width:100%;height: 100%'></iframe>",
            id:"guarantee_project_addUser"
        });
        rightMenu();
        iframeWH();
        parent.layui.element.tabChange('tabs','guarantee_project_addUser');


    })


});
